﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Reflection.Emit;
using System.Text.RegularExpressions;
using System.Configuration;

namespace PBDesk.AutoGen.AutoEnums
{
    class Program
    {
        static void Main(string[] args)
        {
            string strAssemblyName = ConfigurationManager.AppSettings["defaultNamespace"];
            string assemblyFileName = ConfigurationManager.AppSettings["assemblyFileName"];

            AppDomain currentDomain = AppDomain.CurrentDomain;

            string outputDir = currentDomain.BaseDirectory + "..\\..\\..\\Outputs\\";

            AssemblyName asmName = new AssemblyName(strAssemblyName);
            AssemblyBuilder asmBuilder = currentDomain.DefineDynamicAssembly(asmName, AssemblyBuilderAccess.Save, outputDir);

            EnumCreationList list = EnumCreationList.GetList();


            //var t = ConfigurationManager.GetSection("enumConfigs");

            //EnumCreationList l = (EnumCreationList)ConfigurationManager.GetSection("enumConfigs/enum");

            ModuleBuilder moduleBuilder = asmBuilder.DefineDynamicModule(asmName.Name, assemblyFileName);

            EnumCreator.BuildEnum(moduleBuilder, list);
            //EnumCreator.BuildEnum(moduleBuilder, "AuthenticationProviders", "LL_AuthProvider", "AuthProviderId", "AuthProviderName",  typeof(short));
            
            asmBuilder.Save(assemblyFileName);
        }

        

        public static string NormalizeLiteralNames(string enumValueName)
        {
            // Remove invalid characters from everywhere and replace them with_   
            var level1Correction = Regex.Replace(enumValueName, "[^A-Za-z0-9]+", "_");   
            
            // Make sure the name does not begin with a number. if it does add an _ to the beginning   
            var number = 0;   
            var beginsWithNumber = int.TryParse(level1Correction[0].ToString(), out number);   
            return beginsWithNumber ? level1Correction.Insert(0, "_") : level1Correction; 
        }
    }
}
